package br.edu.ufcg.sri.recuperacao.gui;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;

import br.edu.ufcg.sri.armazenamento.ArmazenadorImpl;
import br.edu.ufcg.sri.armazenamento.docs.DocumentoIF;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 *
 *
 * @author ROGRIGO
 *
 */
/**
 * @author ROGRIGO
 *
 */
public class MainGUI {

	private static final long serialVersionUID = -1428200783985487278L;

	/**
	 * JFrame aberto atualmente 
	 */
	private static JFrame janelaAberta;

	/**
	 * T�tulo da janela aberta
	 */
	private static String titulo = "Projeto de SRI";

	/**
	 * 
	 */
	private static Operacao operacao;

	/**
	 * 
	 */

	private static MainGUI instance = null;


	protected MainGUI() {
		operacao = new Operacao(ArmazenadorImpl.getInstance());
//		operacao = new Operacao(new ArmazenadorImplMock());
	}

	public static MainGUI getInstance() {
		if (instance == null)
			instance = new MainGUI();
		return instance;
	}
	
	public static void initMainGUI(){
		if (instance == null){
			instance = getInstance();
		}
		instance.openSearchWindow();
	}

	public void encerra() {
		closeCurrentWindow();
		System.exit(0);

	}

	private static void closeCurrentWindow() {

		if (janelaAberta != null) {
			janelaAberta.setVisible(false);
		}
	}

	private static void openCurrentWindow() {

		if (janelaAberta != null) {
			janelaAberta.setTitle(titulo);
			janelaAberta.validate();
			janelaAberta.setVisible(true);
		}
	}

	private void openSearchWindow() {
		List<String> l = operacao.getTermos();
		Collections.sort(l);
		janelaAberta = new SearchGUI(l);
		openCurrentWindow();
	}

	private static void openRetrievalWindow(Map<String,Set<String>> youmean, List<DocumentoIF> resultados) {

		janelaAberta = new RetrievalGUI(youmean, resultados);
		openCurrentWindow();
	}

	public void search(String[] consulta) {
		Map<String,Set<String>> youmean = getSugestoes(consulta);
		List<DocumentoIF> documentos = fazBusca(consulta);
				
		closeCurrentWindow();
		openRetrievalWindow(youmean, documentos);
	}



	public void back() {
		// inten��o desse m�todo � voltar sempre pra tela anterior, mas
		// s� tem 2 telas aqui, ent�o n�o precisa.
		closeCurrentWindow();
		openSearchWindow();
	}

	// ===vvv=== ALGORITMOS QUE SER�O MUDADOS ===vvv===

	private Map<String,Set<String>> getSugestoes(String[] termosMostrados) {
		
		Map<String,Set<String>> showWords = new HashMap<String,Set<String>>(operacao.getSinonimos(termosMostrados));
		
		//
		//	Espa�o reservado para codifica��o da recupera��o de hipern�mias e hipon�mias 
		//
		return showWords;

	}

	private List<DocumentoIF> fazBusca(String[] consulta) {
		Map<DocumentoIF,Double> sim = operacao.getBuscaVetorial(consulta);
		Set<DocumentoIF> docs = new HashSet<DocumentoIF>(sim.keySet());
		List<DocumentoIF> lista = new LinkedList<DocumentoIF>();
//		System.out.println("docs.size=" + docs.size());
		// 
		while (!docs.isEmpty()) {
			DocumentoIF docMaior = null;
			double atual, max = -1;
			for (DocumentoIF d : docs) {
				
				atual = sim.get(d);
//				System.out.println(atual);
				if (atual > max) {
					max = atual;
					docMaior = d;
				}
			}
//			System.out.println(docMaior + " " + docs.toString());
			if (!docs.remove(docMaior)) throw new RuntimeException("n�o foi poss�vel continuar");
			lista.add(docMaior);
		}
//		System.out.println(sim);
//		System.out.println(lista);
		return lista;
	}
	
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		MainGUI.initMainGUI();
	}

}
